//
//  XVVSelectProductImg.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/1.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVVSelectProductImg.h"
#import "XXTool.h"
#import "QBImagePickerController.h"

@interface XVVSelectProductImg ()<QBImagePickerControllerDelegate>

@property (nonatomic,strong) NSMutableArray *settingArray;  //存放初始的view的数组

@property (nonatomic,strong) NSMutableArray *selectedImgArray; //存放选择图片的数组

@property (nonatomic,strong) NSMutableArray *deleteButtonArray; //存放删除按钮图片的数组

@property (nonatomic,strong) UILabel *tipLabel;

@end

@implementation XVVSelectProductImg
{
    NSInteger maxSelectCount;
    NSInteger nowSelectCount;
    CGFloat imgWidth;
}

-(NSMutableArray*)settingArray
{
    if (_settingArray==nil)
    {
        _settingArray = [[NSMutableArray alloc] init];
        for (int i=0; i<3; i++)
        {
            UIImageView* temp = [[UIImageView alloc] init];
            temp.userInteractionEnabled = YES;
            temp.clipsToBounds = YES;
            temp.contentMode = UIViewContentModeScaleAspectFill;
            if (i==0)
            {
                [temp addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAddImg)]];
                temp.image = [UIImage imageNamed:@"增加图片按钮"];
            }
            else
            {
                temp.image = [UIImage imageNamed:@"虚线框"];
            }
            [self addSubview:temp];
            [_settingArray addObject:temp];
        }
    }
    return _settingArray;
}

-(NSMutableArray*)selectedImgArray
{
    if (_selectedImgArray==nil)
    {
        _selectedImgArray = [[NSMutableArray alloc] init];
    }
    return _selectedImgArray;
}

-(NSMutableArray*)deleteButtonArray
{
    if (_deleteButtonArray==nil)
    {
        _deleteButtonArray = [[NSMutableArray alloc] init];
    }
    return _deleteButtonArray;
}

-(UILabel*)tipLabel
{
    if (_tipLabel==nil)
    {
        _tipLabel = [[UILabel alloc] init];
        _tipLabel.font = [XXTool systemFontOfSize:16];
        _tipLabel.textColor = [XXTool colorWithHexString:@"8B8B8B"];
        _tipLabel.text = NSLocalizedString(@"添加产品图片提示", nil);
        _tipLabel.numberOfLines = 0;
        [self addSubview:_tipLabel];
    }
    return _tipLabel;
}

-(void)initParameter
{
    imgWidth = ([UIScreen mainScreen].bounds.size.width-(15+22)*2)/3;
}

-(void)initUI
{
    self.backgroundColor = [UIColor whiteColor];
    [self deleteButtonArray];
    [self selectedImgArray];
    [self settingArray];
    [self tipLabel];
}

-(void)refreshLayout:(CGRect)frame
{
   
    
    for (int i=0; i<_settingArray.count; i++)
    {
        UIImageView *temp = [_settingArray objectAtIndex:i];
        temp.frame = CGRectMake(15+(imgWidth+22)*i
                                , 11
                                , imgWidth
                                , imgWidth);
        if (i==0)
        {
            _tipLabel.frame = CGRectMake(temp.frame.origin.x
                                         , temp.frame.size.height+temp.frame.origin.y+7
                                         , frame.size.width-30
                                         , [XXTool getTextHeightByText:_tipLabel.text
                                                                  Font:_tipLabel.font
                                                                 Width:frame.size.width-30]);
        }
    }
}

-(void)clickAddImg
{
    maxSelectCount = 3-_selectedImgArray.count;
    
    QBImagePickerController *imagePickerController = [QBImagePickerController new];
    imagePickerController.delegate = self;
    imagePickerController.allowsMultipleSelection = YES;
    imagePickerController.maximumNumberOfSelection = maxSelectCount;
    imagePickerController.showsNumberOfSelectedAssets = YES;
    [[XXTool getViewController:self] presentViewController:imagePickerController animated:YES completion:NULL];
}

+(CGFloat)getHeight
{
    return ([UIScreen mainScreen].bounds.size.width-(15+22)*2)/3+11+[XXTool getTextHeightByText:NSLocalizedString(@"添加产品图片提示", nil)
                                                                                           Font:[XXTool systemFontOfSize:16]
                                                                                          Width:[UIScreen mainScreen].bounds.size.width-30]+7+14;
}

#pragma mark QBImagePickerDelegate
//相册选中图片
-(void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets
{
    
    //[SVProgressHUD show];
    NSInteger nowSelectImgCount = _selectedImgArray.count;  //确定新添加的图片view的x起点位置
    NSMutableArray* imgArray = [[NSMutableArray alloc] init];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i=0; i<assets.count; i++)
        {
            
            ALAssetRepresentation *rep = [assets[i] defaultRepresentation];
            
            UIImage *img = [UIImage
                            imageWithCGImage:[rep fullResolutionImage] scale:rep.scale orientation:(UIImageOrientation)rep.orientation];
            [imgArray addObject:img];
            //[bigImageArray addObject:[DKClippingImage getUseBigImage:img]];
        }
        [imagePickerController dismissViewControllerAnimated:YES completion:nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //根据添加图片的数量将添加图片按钮往右移动
            for (int i=0; i<_settingArray.count; i++)
            {
                UIImageView *temp = _settingArray[i];
                temp.frame = CGRectMake(temp.frame.origin.x+(imgWidth+22)*assets.count
                                           , temp.frame.origin.y
                                           , imgWidth
                                           , imgWidth);
            }
            
            //根据添加图片的数量新建imageView并展示图片
            for (NSInteger i=0; i<imgArray.count; i++)
            {
                UIImageView *temp = [[UIImageView alloc] initWithImage:imgArray[i]];
                temp.contentMode = UIViewContentModeScaleAspectFill;
                temp.clipsToBounds = YES;
                temp.frame = CGRectMake(15+(imgWidth+22)*(i+nowSelectImgCount)
                                        , 11
                                        , imgWidth
                                        , imgWidth);
                [self addSubview:temp];
                [_selectedImgArray addObject:temp];
                
                
                DKButton *delete = [[DKButton alloc] initWithFrame:CGRectMake(temp.frame.origin.x+temp.frame.size.width-3.5-imgWidth*0.2
                                                                              , temp.frame.origin.y+3.5
                                                                              , imgWidth*0.2, imgWidth*0.2)];
                delete.image = [UIImage imageNamed:@"发布产品删除图片"];
                delete.imageSize = CGSizeMake(imgWidth*0.2, imgWidth*0.2);
                delete.imageRenderingMode = DKButtonRenderingModeAlwaysOriginal;
                [delete setColor:[UIColor clearColor] ForState:DKButtonStateNormal];
                delete.tag = i+nowSelectImgCount;
                [delete addTarget:self Select:@selector(deleteImg:) ForEvent:DKButtonEventPressedEnd];
                [self addSubview:delete];
                [_deleteButtonArray addObject:delete];
            }
        });
    });
}

-(void)qb_imagePickerController:(QBImagePickerController *)imagePickerController SelectAssetsCount:(NSInteger)count
{
    nowSelectCount = count;
}

-(BOOL)qb_imagePickerController:(QBImagePickerController *)imagePickerController shouldSelectAsset:(ALAsset *)asset
{
    if (nowSelectCount==maxSelectCount)
    {
        [SVProgressHUD showInfoWithStatus:@"您选择的图片总数不能大于3张~"];
        return NO;
    }
    else
    {
        return YES;
    }
}
-(void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController
{
    [imagePickerController dismissViewControllerAnimated:YES completion:nil];
}

-(void)deleteImg:(DKButton*)button
{
    //根据添加图片的数量将添加图片按钮往左移动
    for (int i=0; i<_settingArray.count; i++)
    {
        UIImageView *temp = _settingArray[i];
        temp.frame = CGRectMake(temp.frame.origin.x-(imgWidth+22)
                                , temp.frame.origin.y
                                , imgWidth
                                , imgWidth);
    }
    [[_selectedImgArray objectAtIndex:button.tag] removeFromSuperview];
    [[_deleteButtonArray objectAtIndex:button.tag] removeFromSuperview];
    [_selectedImgArray removeObjectAtIndex:button.tag];
    [_deleteButtonArray removeObjectAtIndex:button.tag];
    
    for (int i=0; i<_selectedImgArray.count; i++)
    {
        UIImageView *tempImg = [_selectedImgArray objectAtIndex:i];
        DKButton *tempButton = [_deleteButtonArray objectAtIndex:i];
        tempImg.frame = CGRectMake(15+(imgWidth+22)*i
                                   , 11
                                   , imgWidth
                                   , imgWidth);
        tempButton.frame = CGRectMake(tempImg.frame.origin.x+tempImg.frame.size.width-3.5-imgWidth*0.2
                                      , tempImg.frame.origin.y+3.5
                                      , imgWidth*0.2, imgWidth*0.2);
        tempButton.tag = i;
    }
}

#pragma mark api
-(NSArray*)getSelectProductImgArray
{
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    for (int i=0; i<_selectedImgArray.count; i++)
    {
        UIImageView *temp = _selectedImgArray[i];
        [tempArray addObject:temp.image];
    }
    return tempArray;
}

-(void)setRequestSelectedImg:(NSArray *)imgUrlArray
{
    if (_selectedImgArray.count!=0)
    {
        for (UIView* temp in _selectedImgArray)
        {
            [temp removeFromSuperview];
        }
        [_selectedImgArray removeAllObjects];
        
        for (UIView* deleteTemp in _deleteButtonArray)
        {
            [deleteTemp removeFromSuperview];
        }
        [_deleteButtonArray removeAllObjects];
    }
    
    NSInteger selectImgCount = 0;
    for (int i=0; i<imgUrlArray.count; i++)
    {
        if (![imgUrlArray[i] isEqualToString:@""])
        {
            selectImgCount++;
        }
    }
    
    //根据添加图片的数量将添加图片按钮往右移动
    for (int i=0; i<_settingArray.count; i++)
    {
        UIImageView *temp = _settingArray[i];
        temp.frame = CGRectMake(temp.frame.origin.x+(imgWidth+22)*selectImgCount
                                , temp.frame.origin.y
                                , imgWidth
                                , imgWidth);
    }
    
    //根据添加图片的数量新建imageView并展示图片
    for (NSInteger i=0; i<imgUrlArray.count; i++)
    {
        if (![imgUrlArray[i] isEqualToString:@""])
        {
            UIImageView *temp = [[UIImageView alloc] init];
            temp.contentMode = UIViewContentModeScaleAspectFill;
            [temp setImageWithUrl:imgUrlArray[i]];
            temp.clipsToBounds = YES;
            temp.frame = CGRectMake(15+(imgWidth+22)*i
                                    , 11
                                    , imgWidth
                                    , imgWidth);
            [self addSubview:temp];
            [_selectedImgArray addObject:temp];
            
            
            DKButton *delete = [[DKButton alloc] initWithFrame:CGRectMake(temp.frame.origin.x+temp.frame.size.width-3.5-imgWidth*0.2
                                                                          , temp.frame.origin.y+3.5
                                                                          , imgWidth*0.2, imgWidth*0.2)];
            delete.image = [UIImage imageNamed:@"发布产品删除图片"];
            delete.imageSize = CGSizeMake(imgWidth*0.2, imgWidth*0.2);
            delete.imageRenderingMode = DKButtonRenderingModeAlwaysOriginal;
            [delete setColor:[UIColor clearColor] ForState:DKButtonStateNormal];
            delete.tag = i;
            [delete addTarget:self Select:@selector(deleteImg:) ForEvent:DKButtonEventPressedEnd];
            [self addSubview:delete];
            [_deleteButtonArray addObject:delete];
        }
    }
    
}

@end
